#!/bin/bash
myFile=$(find /usr/mcc/update/ -name commandcenter.mcc-smp-web-service.* |xargs ls -alt| head -n 1 | awk '{print $9}' )
echo "target:$myFile"
OPTSTATUS=200
PGIP=0.0.0.0
PGPORT=5432
PGUSER=postgres
PGPWD=123456

function help() {
  echo "Usage:"
  echo "smp.sh [-s command]"
  echo "Description:";
  echo "command:0 unInstall"
  echo "command:1 install"
  echo "command:2 stop"
  echo "command:3 start"
  echo "command:013 unInstall,install,start"
  exit;
}

while getopts ":i:v:p:u:w:s:" opt
do
    case $opt in
        i)
        echo "ip--$OPTARG"
 PGIP=$OPTARG;
        ;;
     v)
        echo "version--$OPTARG"
    SERVERVERSION=V$OPTARG;
        ;;
        p)
        echo "port--$OPTARG"
 PGPORT=$OPTARG;
        ;;
        u)
        echo "user--$OPTARG"
 PGUSER=$OPTARG;
        ;;
        w)
        echo "pwd--$OPTARG"
 PGPWD=$OPTARG;
        ;;
        s)
        echo "OPT--$OPTARG"
 OPTSTATUS=$OPTARG;;
h) help;;
?) help ;

  read -r -p "Are You Sure? [Y/n] " input
    case $input in
    [yY][eE][sS]|[yY])
      echo $input
      ;;
    [nN][oO]|[nN])
      exit 1
    ;;
    *)
    echo "Invalid input..."
    exit 1
    ;;
    esac
        ;;
        ?)
        echo "---$opt"
        exit 1;;
    esac
done

function stop(){
  /usr/mcc/server/mcc-smp-web-service/bin/shutdown.sh
}

function run(){
/usr/mcc/server/mcc-smp-web-service/bin/startup.sh
}

function install(){
  tar -zxvf $myFile -C /usr/mcc/server/
}

function unInstall(){
  rm -rf /usr/mcc/server/mcc-smp-web-service
  rm -rf /usr/mcc/logs/mcc-smp*
}
## 判断是安装还是卸载 0卸载  1 安装  2:停止 3:启动
if [ "$OPTSTATUS" == '0' ];
then
   stop
   unInstall
elif [ "$OPTSTATUS" == '1' ];
  then
    install
elif [ "$OPTSTATUS" == '2' ];
  then
    stop
elif [ "$OPTSTATUS" == '3' ];
  then
    run
elif [ "$OPTSTATUS" == '4' ];
  then
    stop
    unInstall
    install
    run
else
  echo "You can only enter 0:uninstall/1:install/2:stop/3:run/4:unInstall install run"
fi